package cn.wolfcode.cms.service.impl;

import cn.wolfcode.cms.domain.Employee;
import cn.wolfcode.cms.domain.RecordExchange;
import cn.wolfcode.cms.mapper.RecordExchangeMapper;
import cn.wolfcode.cms.query.QueryObject;
import cn.wolfcode.cms.service.IExchangeService;
import cn.wolfcode.cms.service.IPresentService;
import cn.wolfcode.cms.service.IRecordExchangeService;
import cn.wolfcode.cms.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RecordExchangeServiceImpl implements IRecordExchangeService {

    @Autowired
    private IExchangeService exchangeService;

    @Autowired
    private IPresentService presentService;

    @Autowired
    private RecordExchangeMapper recordExchangeMapper;

    public void save(RecordExchange entity,Long mid,Integer prenum,Long preid) {
        //设置录入人
        entity.setInputUser((Employee) SecurityUtils.getSubject().getPrincipal());
        //设置录入时间
        entity.setInputTime(new Date());
        recordExchangeMapper.insert(entity);
        exchangeService.updataBalance(mid,entity.getBalance());
        presentService.updateTotalNum(preid,prenum);
    }

    public RecordExchange get(Long id) {
        return recordExchangeMapper.selectByPrimaryKey(id);
    }

    public List<RecordExchange> list() {
        return recordExchangeMapper.selectAll();
    }

    public Object query(QueryObject qo) {
        //查总数
        int total = recordExchangeMapper.queryForCount(qo);
        if(total == 0){
            return new PageResult();
        }
        //查数据
        List<RecordExchange> rows = recordExchangeMapper.queryForList(qo);
        return new PageResult(total, rows);
    }

}
